from django.template import Library
from django.urls import reverse
from web import models

# 实例化Library，实例化对象名必须为register
register = Library()


# 利用装饰器创建自定义模板标签，为manage模板渲染项目区块显示效果
@register.inclusion_tag('inclusion/all_project_list.html')  # 目录为templates下的inclusion
def all_project_list(request):  # 需要在调用all_project时传入 request
    # 我创建的项目
    my_project_list = models.Project.objects.filter(creator=request.tracer.user)

    # 我参与的项目
    join_project_list = models.ProjectUser.objects.filter(user=request.tracer.user)

    return {'my': my_project_list, 'join': join_project_list}

@register.inclusion_tag('inclusion/project_menu.html')
def project_menu(request):
    # 将所有的扩展菜单地址写入列表，方便循环处理
    data_list = [
        {'title': '概述', 'url': reverse('dashboard', kwargs={'project_id': request.tracer.project.id})},
        {'title': '问题', 'url': reverse('issues', kwargs={'project_id': request.tracer.project.id})},
        {'title': '统计', 'url': reverse('statistics', kwargs={'project_id': request.tracer.project.id})},
        {'title': '文件', 'url': reverse('file', kwargs={'project_id': request.tracer.project.id})},
        {'title': 'wiki', 'url': reverse('wiki', kwargs={'project_id': request.tracer.project.id})},
        {'title': '配置', 'url': reverse('setting', kwargs={'project_id': request.tracer.project.id})},
    ]

    # 通过匹配点击后页面的地址与列表中的元素url，如果一致，增加class=active
    for item in data_list:
        if request.path_info.startswith(item['url']):
            item['class'] = 'active'

    return {'data_list': data_list}
